package com.wy._05Random.randomFile.randomPic.shapeDemo;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;

public class DrawPolygons extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 绘制正方形
        int squareSize = 100;
        Polygon square = new Polygon();
        square.addPoint(getWidth() / 2 - squareSize / 2, getHeight() / 2 - squareSize / 2);
        square.addPoint(getWidth() / 2 + squareSize / 2, getHeight() / 2 - squareSize / 2);
        square.addPoint(getWidth() / 2 + squareSize / 2, getHeight() / 2 + squareSize / 2);
        square.addPoint(getWidth() / 2 - squareSize / 2, getHeight() / 2 + squareSize / 2);
        g.setColor(Color.RED);
        g.fillPolygon(square);

        // 绘制三角形
        int triangleHeight = 75;
        int triangleBase = 150;
        Polygon triangle = new Polygon();
        triangle.addPoint(getWidth() / 2 - triangleBase / 2, getHeight() / 2 - triangleHeight / 2);
        triangle.addPoint(getWidth() / 2 + triangleBase / 2, getHeight() / 2 - triangleHeight / 2);
        triangle.addPoint(getWidth() / 2, getHeight() / 2 + triangleHeight / 2);
        g.setColor(Color.GREEN);
        g.fillPolygon(triangle);

        // 绘制六边形
        int hexagonRadius = 60;
        int numSides = 6;
        Polygon hexagon = new Polygon();
        for (int i = 0; i < numSides; i++) {
            double angle = 2 * Math.PI * i / numSides;
            int x = (int) (getWidth() / 2 + hexagonRadius * Math.cos(angle));
            int y = (int) (getHeight() / 2 + hexagonRadius * Math.sin(angle));
            hexagon.addPoint(x, y);
        }
        g.setColor(Color.BLUE);
        g.fillPolygon(hexagon);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw Polygons Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.add(new DrawPolygons());
        frame.setVisible(true);
    }
}
